package com.zjuee.web.tporder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.utils.DesensitizationUtil;
import com.zjuee.base.dto.common.tporder.TransAffairsApplyDto;
import com.zjuee.base.model.common.tporder.WkTransAffairsApplyEntity;
import com.zjuee.base.model.common.tporder.WkTransAffairsCompanyEntity;
import com.zjuee.base.model.common.tporder.WkTransAffairsFileEntity;
import com.zjuee.base.model.common.tporder.WkTransAffairsMaterialEntity;
import com.zjuee.web.tporder.mapper.TransAffairsApplyMapper;
import com.zjuee.web.tporder.mapper.TransAffairsCompanyMapper;
import com.zjuee.web.tporder.mapper.TransAffairsFileMapper;
import com.zjuee.web.tporder.mapper.TransAffairsMaterialMapper;
import com.zjuee.service.TransAffairsApplyService;
import com.zjuee.vo.WkTransAffairsApplyVo;
import com.zjuee.vo.WkTransAffairsCompanyVo;
import com.zjuee.vo.WkTransAffairsMaterialVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class TransAffairsApplyImpl implements TransAffairsApplyService {
    @Resource
    private TransAffairsApplyMapper transAffairsApplyMapper;
    @Resource
    private TransAffairsCompanyMapper transAffairsCompanyMapper;
    @Resource
    private TransAffairsMaterialMapper transAffairsMaterialMapper;
    @Resource
    private TransAffairsFileMapper transAffairsFileMapper;
    @Resource
    private LoginDto loginDto;

    @Override
    public Long add(WkTransAffairsApplyVo type) {
        WkTransAffairsApplyEntity wkTransAffairsApplyEntity = new WkTransAffairsApplyEntity();
        BeanUtils.copyProperties(type, wkTransAffairsApplyEntity);
        //计划开始日期
        wkTransAffairsApplyEntity.setWtaaKsrq(new Date());
        //计划结束日期
        wkTransAffairsApplyEntity.setWtaaJsrq(new Date());
        //商请日期
        wkTransAffairsApplyEntity.setWtaaSqrq(new Date());
        //商请结果
        wkTransAffairsApplyEntity.setWtaaSqjg("待商情");
        //是否同意 0：未回复  1：同意  2：不同意
        wkTransAffairsApplyEntity.setWtaaHfbj(0);
        //审批结果：待审批/审批通过/审批不通过
        wkTransAffairsApplyEntity.setWtaaSpjq("待审批");
        //是否审批 0：未审核  1：同意  2：不同意
        wkTransAffairsApplyEntity.setWtaaSpbj(0);
        //是否上传全国平台  0：未上传 1：已上传 2: 更新上传
        wkTransAffairsApplyEntity.setWtaaIsUpload(0);
        //是否上传省平台  0：未上传 1：已上传
        wkTransAffairsApplyEntity.setWtaaIsUploadProvince(0);
        //数据来源 0：省平台
//        wkTransAffairsApplyEntity.setWtaaSource("0");
        transAffairsApplyMapper.insert(wkTransAffairsApplyEntity);
        List<WkTransAffairsCompanyVo> wkTransAffairsCompanyVos = type.getWkTransAffairsCompanyVos();
        for(WkTransAffairsCompanyVo wkTransAffairsCompanyVo : wkTransAffairsCompanyVos){
            WkTransAffairsCompanyEntity wkTransAffairsCompanyEntity = new WkTransAffairsCompanyEntity();
            BeanUtils.copyProperties(wkTransAffairsCompanyVo, wkTransAffairsCompanyEntity);
            wkTransAffairsCompanyEntity.setWtaaId(wkTransAffairsApplyEntity.getId());
            transAffairsCompanyMapper.insert(wkTransAffairsCompanyEntity);
        }

        List<WkTransAffairsMaterialVo> wkTransAffairsMaterialVos = type.getWkTransAffairsMaterialVos();
        for(WkTransAffairsMaterialVo wkTransAffairsMaterialVo : wkTransAffairsMaterialVos){
            WkTransAffairsMaterialEntity wkTransAffairsMaterialEntity = new WkTransAffairsMaterialEntity();
            BeanUtils.copyProperties(wkTransAffairsMaterialVo, wkTransAffairsMaterialEntity);
            wkTransAffairsMaterialEntity.setWtaaId(wkTransAffairsApplyEntity.getId());
            transAffairsMaterialMapper.insert(wkTransAffairsMaterialEntity);
        }
        return wkTransAffairsApplyEntity.getId();
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        return false;
    }

    @Override
    public boolean update(WkTransAffairsApplyVo type) {
        return false;
    }

    @Override
    public WkTransAffairsApplyVo selectById(Long id, Long validateId) {
        return null;
    }

    @Override
    public Page<WkTransAffairsApplyVo> list(TransAffairsApplyDto dto) {
        dto.setBciId(loginDto.getOrgBusinessId());
        Page<WkTransAffairsApplyVo> page = transAffairsApplyMapper.getTransAffairsApplyList(dto.getPage().convert(), dto);
        page.getRecords().forEach(p -> {
            p.setWtaaSqrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.getWtaaSqrq()));
            if (p.getWtaaHfrq() != null){
                p.setWtaaHfrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.getWtaaHfrq()));
            }
            p.setWtaaKsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.getWtaaKsrq()));
            p.setWtaaJsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.getWtaaJsrq()));
            p.setWtaaWfycdwlxr(DesensitizationUtil.desensitizedName(p.getWtaaWfycdwlxr()));
            p.setWtaaFwycdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(p.getWtaaFwycdwlxrsj()));
            p.setWtaaWfjsdwlxr(DesensitizationUtil.desensitizedName(p.getWtaaWfjsdwlxr()));
            p.setWtaaWfjsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(p.getWtaaWfjsdwlxrsj()));
        });
        return page;
    }

    @Override
    public LambdaQueryWrapper<WkTransAffairsApplyVo> fuzzyQuery(TransAffairsApplyDto dto) {
        return null;
    }

    @Override
    public WkTransAffairsApplyVo getTransAffairsApply(Long id) {
        WkTransAffairsApplyEntity wkTransAffairsApplyEntity = transAffairsApplyMapper.selectById(id);

        QueryWrapper<WkTransAffairsCompanyEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("wtaa_id", id);
        List<WkTransAffairsCompanyEntity> wkTransAffairsCompanyEntitys = transAffairsCompanyMapper.selectList(queryWrapper1);

        QueryWrapper<WkTransAffairsMaterialEntity> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("wtaa_id", id);
        List<WkTransAffairsMaterialEntity> wkTransAffairsMaterialEntitys = transAffairsMaterialMapper.selectList(queryWrapper2);

        QueryWrapper<WkTransAffairsFileEntity> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("wta_id", id);
        List<WkTransAffairsFileEntity> wkTransAffairsFileEntities = transAffairsFileMapper.selectList(queryWrapper3);

        WkTransAffairsApplyVo wkTransAffairsApplyVo =  new WkTransAffairsApplyVo();

        List<WkTransAffairsCompanyVo> wkTransAffairsCompanyVos = new ArrayList();

        List<WkTransAffairsMaterialVo> wkTransAffairsMaterialVos = new ArrayList();

        BeanUtils.copyProperties(wkTransAffairsApplyEntity, wkTransAffairsApplyVo);
        for(WkTransAffairsCompanyEntity wkTransAffairsCompanyEntity : wkTransAffairsCompanyEntitys){
            WkTransAffairsCompanyVo wkTransAffairsCompanyVo = new WkTransAffairsCompanyVo();
            BeanUtils.copyProperties(wkTransAffairsCompanyEntity, wkTransAffairsCompanyVo);
            wkTransAffairsCompanyVo.setWtacYsdwlxr(DesensitizationUtil.desensitizedName(wkTransAffairsCompanyVo.getWtacYsdwlxr()));
            wkTransAffairsCompanyVo.setWtacYsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransAffairsCompanyVo.getWtacYsdwlxrsj()));
            wkTransAffairsCompanyVos.add(wkTransAffairsCompanyVo);
        }
        for(WkTransAffairsMaterialEntity wkTransAffairsMaterialEntity : wkTransAffairsMaterialEntitys){
            WkTransAffairsMaterialVo wkTransAffairsMaterialVo = new WkTransAffairsMaterialVo();
            BeanUtils.copyProperties(wkTransAffairsMaterialEntity, wkTransAffairsMaterialVo);
            wkTransAffairsMaterialVos.add(wkTransAffairsMaterialVo);
        }

        wkTransAffairsApplyVo.setWtaaSqrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransAffairsApplyVo.getWtaaSqrq()));
        if (wkTransAffairsApplyVo.getWtaaHfrq() != null){
            wkTransAffairsApplyVo.setWtaaHfrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransAffairsApplyVo.getWtaaHfrq()));
        }
        wkTransAffairsApplyVo.setWtaaKsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransAffairsApplyVo.getWtaaKsrq()));
        wkTransAffairsApplyVo.setWtaaJsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransAffairsApplyVo.getWtaaJsrq()));
        wkTransAffairsApplyVo.setWkTransAffairsCompanyVos(wkTransAffairsCompanyVos);
        wkTransAffairsApplyVo.setWkTransAffairsMaterialVos(wkTransAffairsMaterialVos);
        wkTransAffairsApplyVo.setWkTransAffairsFileEntities(wkTransAffairsFileEntities);
        //脱敏处理
        wkTransAffairsApplyVo.setWtaaWfycdwlxr(DesensitizationUtil.desensitizedName(wkTransAffairsApplyVo.getWtaaWfycdwlxr()));
        wkTransAffairsApplyVo.setWtaaFwycdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransAffairsApplyVo.getWtaaFwycdwlxrsj()));
        wkTransAffairsApplyVo.setWtaaWfjsdwlxr(DesensitizationUtil.desensitizedName(wkTransAffairsApplyVo.getWtaaWfjsdwlxr()));
        wkTransAffairsApplyVo.setWtaaWfjsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransAffairsApplyVo.getWtaaWfjsdwlxrsj()));

        return wkTransAffairsApplyVo;
    }
}
